package com.emapp.lccode.activity

import android.os.Bundle
import android.view.View
import com.emapp.lccode.R
import com.emapp.lccode.adapter.MaterialSelectAdapter
import com.emapp.lccode.adapter.MaterialSelectInforAdapter
import com.emapp.lccode.adapter.OrderImageKtAdapter
import com.emapp.lccode.base.BaseActivity1
import com.emapp.lccode.base.BaseApplication
import com.emapp.lccode.base.BaseConfig
import com.emapp.lccode.base.EventBusConfig
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.bean.OrderKt
import com.emapp.lccode.databinding.ActivityReportInforBinding
import com.emapp.lccode.mode.OrderKtViewModel
import com.emapp.lccode.utils.DividerOrientation
import com.emapp.lccode.utils.GlideEngine
import com.emapp.lccode.utils.divider
import com.emapp.lccode.utils.getColorExt
import com.emapp.lccode.utils.grid
import com.emapp.lccode.utils.setOnclickNoRepeat
import com.emapp.lccode.utils.toStartActivity
import com.emapp.lccode.utils.verticalnoScroll
import com.gyf.immersionbar.ktx.immersionBar
import com.hjq.toast.Toaster
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.style.PictureSelectorStyle
import me.hgj.jetpackmvvm.ext.parseState
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 我发布的工单详情
 */
class ReportInforKtActivity : BaseActivity1<OrderKtViewModel, ActivityReportInforBinding>() {
    var id = ""
    private val imageAdapter: OrderImageKtAdapter by lazy { OrderImageKtAdapter() }
    var images: ArrayList<LocalMedia> = arrayListOf()
    var order: OrderKt? = null
    private val materialAdapter: MaterialSelectInforAdapter by lazy { MaterialSelectInforAdapter() }
    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            titleBar(mViewBind.lvTitle.ivBar)
            statusBarDarkFont(true)
            fullScreen(true)
            statusBarColor(R.color.transparent)
            navigationBarColor(R.color.white)
        }
        mViewBind.lvTitle.tvTitle.text = "详情"
        mViewBind.lvTitle.tvRight.visibility = View.GONE

        EventBus.getDefault().register(this)
        id = intent.extras?.getString("id", "") ?: ""
        order = intent.extras?.getSerializable("order") as OrderKt?
        //log_e("order---" + order?.id)
        mViewBind.rvList.run {
            grid(4)
            adapter = imageAdapter
            divider {
                orientation = DividerOrientation.HORIZONTAL
                includeVisible = true
                setDivider(10, true)
                setColor(getColorExt(R.color.white))
            }
        }
        imageAdapter.run {
            setOnItemClickListener { adapter, view, position ->
                PictureSelector.create(this@ReportInforKtActivity)
                    .openPreview()
                    .setImageEngine(GlideEngine.createGlideEngine())
                    .setSelectorUIStyle(PictureSelectorStyle())
                    .isPreviewFullScreenMode(true)
                    .isHidePreviewDownload(true) //.setExternalPreviewEventListener(new MyExternalPreviewEventListener(adapter))
                    .startActivityPreview(position, false, images)
            }
        }
        mViewModel.getOrderInfor(id)
        if (order?.check_status == 0) {//待派单
            mViewBind.btCancel.visibility = View.GONE//
            mViewBind.btOk.visibility = View.GONE//
        } else if (order?.check_status == 1) {//1审核中(管理员已经派单了)
            if (order?.checkIds?.contains(BaseApplication.instance.adminId.toInt()) == true) {//是执行者
                mViewBind.btCancel.visibility = View.GONE//
                mViewBind.btOk.visibility = View.VISIBLE//
            } else {//非执行者
                mViewBind.btCancel.visibility = View.GONE//
                mViewBind.btOk.visibility = View.GONE//
            }
        } else if (order?.check_status == 2) {//已经回复了
            mViewBind.btCancel.visibility = View.GONE//
            mViewBind.btOk.visibility = View.GONE//
            if (!order?.bomArr.isNullOrEmpty()) {
                mViewBind.lvWuliao.visibility = View.VISIBLE//
                mViewBind.rvList2.run {
                    verticalnoScroll()
                    adapter = materialAdapter
                }
                materialAdapter.submitList(order?.bomArr)
                materialAdapter.notifyDataSetChanged()
            }
        } else {////3审核不通过,4撤销审核
            mViewBind.btCancel.visibility = View.GONE//
            mViewBind.btOk.visibility = View.GONE//
        }
    }

    override fun createObserver() {
        mViewModel.run {
            orderInfor.observe(this@ReportInforKtActivity) { result ->
                parseState(result, {
                    setInfor(it[0])
                }, {
                    Toaster.show(it.errorMsg)
                })

            }
        }
    }

    private fun setInfor(infor: OrderKt) {
        mViewBind.tvName.text = infor.name
        mViewBind.tvNumber.text = infor.num_code
        mViewBind.tvAddress.text = infor.address
        mViewBind.tvContent.text = infor.content
        mViewBind.tvRemark.text = infor.remark
        if (infor.fileArray != null) {
            images.clear()
            infor.fileArray.forEach {
                var l = LocalMedia()
                l.path = BaseConfig.ROOT_URL + it.filepath
                images.add(l)
            }
            imageAdapter.submitList(images)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(
            mViewBind.lvTitle.ivLeft,
            mViewBind.lvTitle.tvRight,
            mViewBind.btCancel,
            mViewBind.btOk
        ) {
            when (it.id) {
                R.id.iv_left -> {
                    finish()
                }

                R.id.bt_cancel -> {
                }

                R.id.bt_ok -> {
                    var bundle = Bundle()
                    bundle.putString("id", order?.id)
                    toStartActivity(OrderReplyKtActivity::class.java, bundle)
                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(event: EventBusModel) {
        when (event.type) {
            EventBusConfig.REFRESH_ORDER -> {
                finish()
            }

            else -> {}
        }
    }

}
